package com.gexiaobao.pigeon.ui.auction

import android.annotation.SuppressLint
import android.graphics.Paint
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.RxTextTool
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.model.bean.CustomPrice
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.SettingUtil
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityProxyBidBinding
import com.gexiaobao.pigeon.ui.activity.ActivityDeliveryAddress
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.viewmodel.AuctionViewModel
import com.google.gson.Gson
import me.hgj.jetpackmvvm.ext.lifecycle.KtxActivityManger
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity
import me.hgj.jetpackmvvm.util.startActivityForResult
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.text.DecimalFormat
import kotlin.math.ceil

/**
 * @Author      : hxw
 * @Date        : 2024/11/12 17:03
 * @Describe    : 代理出价
 */
class ActivityProxyBid : BaseActivity<AuctionViewModel, ActivityProxyBidBinding>() {

    private var intentType = 0
    private var t7lUrl: String = ""
    private var mAddressDesc: String = ""
    private var mAddressName: String = ""
    private var mAddressPhone: String = ""
    private var mProxyId: Int = 0
    private var proxyType: String = ""
    private var currentLatestPrice = 0
    private var customOverPrice = 0
    private var customPrices: ArrayList<CustomPrice> = arrayListOf()
    private var mGoodId = 0
    private var mPrice = 0
    private var bondPercent = 0
    private var fixedMarkupAmount = 0//固定加价金额

    /** 2580规则 */
    private val incrementsTFE = intArrayOf(200, 500, 800, 1000, 1200, 1500, 1800, 2000, 2200, 2500, 2800, 3000) // 递增的数值(千单位)
    private val incrementsTTFE = intArrayOf(10000, 12000, 15000, 18000, 20000, 22000, 25000, 28000, 30000) // 递增的数值(万单位)
    private val incrementsHTTFE = intArrayOf(100000, 120000, 150000, 180000, 200000, 220000, 250000, 280000, 300000) // 递增的数值(十万单位)

    /** 250规则 */
    private val incrementsTHAF = intArrayOf(200, 500, 1000, 1200, 1500, 2000, 2200, 2500, 3000) // 递增的数值(千单位)
    private val incrementsTHAFT = intArrayOf(10000, 12000, 15000, 20000, 22000, 25000, 30000) // 递增的数值(万单位)
    private val incrementsTHAFHT = intArrayOf(100000, 120000, 150000, 200000, 220000, 250000, 300000) // 递增的数值(十万单位)
    private var markupModel = 0//1-默认阶梯加价规则; 2-固定加价规则; 3-2580加价规则; 4-250加价规则; 5-自定义加价规则

    @SuppressLint("SetTextI18n")
    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView(savedInstanceState: Bundle?) {
        KtxActivityManger.pushActivity(this)
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.toolBarLayout.setBackgroundColor(getColor(R.color.color_99CCFF))
        mDatabind.includeView.tvGoToMargin.paint.flags = Paint.UNDERLINE_TEXT_FLAG//设置下划线
        mDatabind.includeBar.tvTitle.text = "代理出价"
        mDatabind.includeView.tvProxyPrice.text = "确认代理出价"
        mDatabind.includeView.tvAuctionCommitmentLetter.text = "《代理出价须知》"
        mDatabind.includeView.tvNowPrice.isVisible = false
        intentType = intent.getIntExtra("type", 0)
        showLoading(getString(R.string.loading))
        mViewModel.getAuctionAddress()

        eventViewModel.toRefreshData.observeInActivity(this) {
            mViewModel.getAuctionAddress()
            mViewModel.getTotalFreezeMoney()
        }

        fixedMarkupAmount = intent.getIntExtra("nextPrice", 0)
        bondPercent = intent.getIntExtra("bondPercent", 0)
        markupModel = intent.getIntExtra("addPriceRule", 0)
        mGoodId = intent.getIntExtra("goodId", 0)
        mPrice = intent.getIntExtra("price", 0)
        t7lUrl = intent.getStringExtra("t7lUrl").toString()
        ImageLoaderManager.loadRoundImage(this, t7lUrl, mDatabind.includeView.ivBiddingPricePigeonImage, 6)
        if (markupModel == 5) {
            customPrices = intent.getParcelableArrayListExtra("customPrices")!!
            customOverPrice = intent.getIntExtra("customOverPrice", 0)
        }
        val orgName = intent.getStringExtra("orgName")
        mDatabind.includeView.tvBiddingPriceOrgName.text = orgName
        currentLatestPrice = mPrice
        mDatabind.includeView.etBiddingPrice.text = formatAmount(mPrice.toDouble())

        mDatabind.includeView.tvMarginPercent.text = "1:$bondPercent"

        mProxyId = intent.getIntExtra("proxyId", 0)
        proxyType = intent.getStringExtra("proxyType").toString()
        if (proxyType == "1") {
            mDatabind.includeView.tvProxyPrice.text = "修改代理出价"
            showLoading(getString(R.string.loading))
            mViewModel.getProxyApp(mProxyId.toString())
            mDatabind.includeView.tvCancelProxyPrice.isVisible = true
        }

        /** 刷新保证金缴纳金额 */
        refreshTheMarginPaymentAmount()

        showLoading(getString(R.string.loading))
        mViewModel.getTotalFreezeMoney()
        mDatabind.swipeRefreshLayout.init {
            mViewModel.getTotalFreezeMoney()
        }

        mDatabind.includeView.tvProxyMinus.setOnClickListener {
            minusAndPlusPrice(1)
        }
        mDatabind.includeView.tvProxyPlus.setOnClickListener {
            minusAndPlusPrice(2)
        }
        setNextPrice(mPrice)
    }

    private fun formatAmount(amount: Double): String {
        val formatter = DecimalFormat("#,###")
        return formatter.format(amount)
    }

    private fun formatBondAmount(amount: Double): String {
        val formatter = DecimalFormat("#,###.00")
        return formatter.format(amount)
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeBar.ivBack,
            mDatabind.includeView.tvProxyPrice,
            mDatabind.includeView.tvAuctionCommitmentLetter,
            mDatabind.includeView.tvGoToMargin,
            mDatabind.includeView.tvCancelProxyPrice,
            mDatabind.includeView.tvChangeAuctionAddress,
            mDatabind.includeView.ivBiddingPricePigeonImage
        ) { it ->
            when (it) {
                mDatabind.includeView.ivBiddingPricePigeonImage -> openBigImage(mDatabind.includeView.ivBiddingPricePigeonImage, t7lUrl)
                mDatabind.includeView.tvChangeAuctionAddress -> startActivityForResult<ActivityDeliveryAddress>("type" to 2) {
                    if (it != null) {
                        val addressDesc = it.getStringExtra("addressDesc").toString()
                        val name = it.getStringExtra("name").toString()
                        val phone = it.getStringExtra("phone").toString()
                        val addressId = it.getIntExtra("addressId", 0)
                        setAddressInfo(addressDesc, name, phone)

                        val map = mapOf("addressId" to addressId, "isSetDefault" to false)
                        val body: RequestBody = Gson().toJson(map).toRequestBody("application/json".toMediaTypeOrNull())
                        mViewModel.setAuctionAddress(body)
                    }
                }

                mDatabind.includeView.tvCancelProxyPrice -> showCancelProxy()
                mDatabind.includeView.tvGoToMargin -> startActivity<ActivityMarginPayment>()
                mDatabind.includeView.tvAuctionCommitmentLetter -> {}
                mDatabind.includeView.tvProxyPrice -> proxyPriceToPay()
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setAddressInfo(mAddressDesc: String, mAddressName: String, mAddressPhone: String) {
        mDatabind.includeView.tvAuctionAddress.text = mAddressDesc
        mDatabind.includeView.tvAuctionContactInfo.text = "$mAddressName   $mAddressPhone"
        if (mAddressName.isEmpty()) {
            mDatabind.includeView.tvChangeAuctionAddress.text = "设置地址"
            mDatabind.includeView.llAuctionAddressInfo.isGone = true
            mDatabind.includeView.tvAuctionAddressNoData.isGone = false
        } else {
            mDatabind.includeView.tvChangeAuctionAddress.text = "修改地址"
            mDatabind.includeView.llAuctionAddressInfo.isGone = false
            mDatabind.includeView.tvAuctionAddressNoData.isGone = true
        }
    }

    private fun showCancelProxy() {
        val dialog = RxDialogDefault(this)
        dialog.setContent("确定要取消代理出价吗?")
        dialog.setSureListener {
            dialog.dismiss()
            showLoading(getString(R.string.loading))
            mViewModel.delProxyApp(mProxyId.toString())
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    @SuppressLint("SetTextI18n")
    private fun refreshTheMarginPaymentAmount() {
        val price = currentLatestPrice
        val result = SettingUtil.div(price.toDouble(), bondPercent.toDouble(), 2)
        val bond = if (result < 999) result else formatBondAmount(result)
        mDatabind.includeView.tvBondPercent.text = "需要缴纳保证金: ￥$bond"
    }

    private fun proxyPriceToPay() {
        if (mDatabind.includeView.tvAuctionContactInfo.text.toString().trim().isEmpty()) {
            RxToast.warning("未设置收货地址")
            return
        }
        val dialog = RxDialogDefault(this)
        dialog.setTitle("确定代理出价吗?")
        dialog.setSureListener {
            showLoading(getString(R.string.loading))
            if (proxyType == "1") {
                val map = mapOf("maxPrice" to currentLatestPrice, "proxyId" to mProxyId)
                val body: RequestBody = Gson().toJson(map).toRequestBody("application/json".toMediaTypeOrNull())
                mViewModel.editProxyApp(body)
            } else {
                val map = mapOf("goodId" to mGoodId, "proxyPrice" to currentLatestPrice)
                val body: RequestBody = Gson().toJson(map).toRequestBody("application/json".toMediaTypeOrNull())
                mViewModel.proxyPrice(body)
            }
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取保证金信息 */
        mViewModel.freezeMoneyResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            dismissLoading()
            RxTextTool.getBuilder("当前保证金余额: ￥ ")
                .append(it.remainMoney.toString()).setAppearance(56).setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark)).setBold()
                .into(mDatabind.includeView.tvMarginBalance)
        }
        /** 获取用户拍卖收货地址 */
        mViewModel.auctionAddressResult.observe(this) {
            mAddressDesc = it.addressDesc
            mAddressName = it.addressName
            mAddressPhone = it.addressPhone
            setAddressInfo(mAddressDesc, mAddressName, mAddressPhone)
        }
        /** 编辑代理 */
        mViewModel.editProxyAppResult.observe(this) {
            RxToast.success("修改成功")
        }
        /** 取消代理 */
        mViewModel.delProxyAppResult.observe(this) {
            eventViewModel.toRefreshData.value = true
            mDatabind.includeView.tvCancelProxyPrice.isVisible = false
            finish()
        }
        /** 获取代理详情 */
        mViewModel.getProxyAppResult.observe(this) {
            mProxyId = it.id
            mPrice = it.maxPrice
            currentLatestPrice = mPrice
            mDatabind.includeView.etBiddingPrice.text = formatAmount(mPrice.toDouble())
            setNextPrice(mPrice)
        }
        /** 代理出价 */
        mViewModel.proxyPriceResult.observe(this) {
            if (it.isSuccess) {
                KtxActivityManger.finishActivity(ActivityAuctionPigeonDetail::class.java)
                KtxActivityManger.finishActivity(ActivityBiddingPrice::class.java)
                startActivity<ActivitySuccessfulBid>("type" to intentType)
                finish()
            } else {
                if (it.errorCode == 6000) {
                    showDialogUnderMargin(it.errorMsg)
                }else{
                    RxToast.error(it.errorMsg)
                }
            }
        }
    }

    private fun showDialogUnderMargin(msg: String) {
        val dialog = RxDialogDefault(this)
        dialog.setTitle("$msg,是否去充值?")
        dialog.setCancelListener {
            dialog.dismiss()
        }
        dialog.setSureListener {
            val map = mapOf("money" to 10000)
            val body: RequestBody = Gson().toJson(map).toRequestBody("application/json".toMediaTypeOrNull())
            showLoading(getString(R.string.loading))
            mViewModel.applyPayDeposit(body)
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    /** 显示下次的加拍价格 */
    private fun setNextPrice(price: Int) {
        val model: String
        val nextPrice: Int
        when (markupModel) {
            2 -> {
                model = "默认阶梯"
                nextPrice = getNextPrice(price)
            }

            1 -> {
                model = "固定"
                nextPrice = getFixedNextPrice(price)
            }

            3 -> {
                model = "2580"
                nextPrice = getTFENextPrice(price)
            }

            4 -> {
                model = "250"
                nextPrice = getTHFNextPrice(price)
            }

            else -> {
                model = "自定义加价"
                nextPrice = getCustomPrices(price)
            }
        }
        mDatabind.includeView.tvBiddingNextPrice.text = model
        mDatabind.includeView.tvNextAddPrice.text = formatAmount(nextPrice.toDouble())
    }

    private fun minusAndPlusPrice(type: Int) {//1 减去，2-加上
        when (markupModel) {//1-默认阶梯加价规则; 2-固定加价规则; 3-2580加价规则; 4-250加价规则; 5-自定义加价规则
            2 -> defaultLadderMarkupRule(type)
            1 -> fixedMarkupRules(type)
            3 -> markupRulesTFZ(type)
            4 -> markupRulesTHAF(type)
            else -> customAddPrice(type)
        }
        refreshTheMarginPaymentAmount()
    }

    private fun getCustomPrices(price: Int): Int {
        var result = 0
        if (price > customPrices[customPrices.size - 1].endPrice) {
            result = price + customOverPrice
        } else {
            if (customPrices.isNotEmpty()) {
                for (i in 0 until customPrices.size) {
                    val startPrice = customPrices[i].startPrice
                    val endPrice = customPrices[i].endPrice
                    if (price in startPrice until endPrice) {
                        result = price + customPrices[i].innerAddPrice
                        break
                    }
                }
            }
        }
        return result
    }

    private fun customAddPrice(type: Int) {
        var value = currentLatestPrice
        when (type) {
            1 -> {
                if (value == mPrice) {
                    RxToast.showToast("当前已是商品底价")
                    return
                }
                if (value > customPrices[customPrices.size - 1].endPrice) {
                    value -= customOverPrice
                } else {
                    if (customPrices.isNotEmpty()) {
                        for (i in 0 until customPrices.size) {
                            val startPrice = customPrices[i].startPrice
                            val endPrice = customPrices[i].endPrice
                            if (value in startPrice..endPrice) {
                                value -= customPrices[i].innerAddPrice
                                break
                            }
                        }
                    }
                }
            }

            else -> {
                if (customPrices.isNotEmpty()) {
                    if (value >= customPrices[customPrices.size - 1].endPrice) {
                        value += customOverPrice

                    } else {
                        for (i in 0 until customPrices.size) {
                            val startPrice = customPrices[i].startPrice
                            val endPrice = customPrices[i].endPrice
                            if (value in startPrice until endPrice) {
                                value += customPrices[i].innerAddPrice
                                break
                            }
                        }
                    }
                }
            }
        }
        currentLatestPrice = value
        mDatabind.includeView.etBiddingPrice.text = formatAmount(value.toDouble())
        setNextPrice(value)
    }

    private fun getTHFNextPrice(price: Int): Int {
        val result: Int
        if (price < 3000) {
            //200→500→1000→1200→1500→2000→2200→2500→3000
            if (price < 200) {
                result = incrementsTHAF[0]
            } else if (price in 200..499) {
                result = incrementsTHAF[1]
            } else if (price in 500..999) {
                result = incrementsTHAF[2]
            } else if (price in 1000..1199) {
                result = incrementsTHAF[3]
            } else if (price in 1100..1499) {
                result = incrementsTHAF[4]
            } else if (price in 1500..1999) {
                result = incrementsTHAF[5]
            } else if (price in 2000..2199) {
                result = incrementsTHAF[6]
            } else if (price in 2200..2499) {
                result = incrementsTHAF[7]
            } else {
                result = incrementsTHAF[8]
            }
        } else if (price in 3000..4999) {
            result = price + 500
        } else if (price in 5000..9999) {
            result = price + 1000
        } else if (price in 10000..29999) {
            //10000→12000→15000→20000→22000→25000→30000
            result = when (price) {
                in 10000..11999 -> incrementsTHAFT[1]
                in 12000..14999 -> incrementsTHAFT[2]
                in 15000..19999 -> incrementsTHAFT[3]
                in 20000..21999 -> incrementsTHAFT[4]
                in 22000..24999 -> incrementsTHAFT[5]
                in 25000..29999 -> incrementsTHAFT[6]
                else -> incrementsTHAFT[7]
            }
        } else if (price in 30000..49999) {
            result = price + 5000
        } else if (price in 50000..99999) {
            result = price + 10000
        } else if (price in 100000..299999) {
            //100000→120000→150000→200000→220000→250000→300000
            result = when (price) {
                in 100000..119999 -> incrementsTHAFHT[1]
                in 120000..149999 -> incrementsTHAFHT[2]
                in 150000..199999 -> incrementsTHAFHT[3]
                in 200000..219999 -> incrementsTHAFHT[4]
                in 220000..249999 -> incrementsTHAFHT[5]
                else -> incrementsTHAFHT[6]
            }
        } else if (price in 300000..499999) {
            result = price + 50000
        } else {
            result = price + 100000
        }
        return result
    }

    private fun getTFENextPrice(price: Int): Int {
        //200→500→800→1000→1200→1500→1800→2000→2200→2500→2800→3000
        val result: Int
        if (price < 3000) {
            if (price < 200) {
                result = incrementsTFE[0]
            } else if (price in 200..499) {
                result = incrementsTFE[1]
            } else if (price in 500..799) {
                result = incrementsTFE[2]
            } else if (price in 800..999) {
                result = incrementsTFE[3]
            } else if (price in 1000..1199) {
                result = incrementsTFE[4]
            } else if (price in 1200..1499) {
                result = incrementsTFE[5]
            } else if (price in 1500..1799) {
                result = incrementsTFE[6]
            } else if (price in 1800..1999) {
                result = incrementsTFE[7]
            } else if (price in 2000..2199) {
                result = incrementsTFE[8]
            } else if (price in 2200..2499) {
                result = incrementsTFE[9]
            } else if (price in 2500..2799) {
                result = incrementsTFE[10]
            } else {
                result = incrementsTFE[11]
            }
        } else if (price in 3000..4999) {
            result = price + 500
        } else if (price in 5000..9999) {
            result = price + 1000
        } else if (price in 10000..29999) {
            //10000→12000→15000→18000→20000→22000→25000→28000→30000
            result = when (price) {
                in 10000..11999 -> incrementsTTFE[1]
                in 12000..14999 -> incrementsTTFE[2]
                in 15000..17999 -> incrementsTTFE[3]
                in 18000..19999 -> incrementsTTFE[4]
                in 20000..21999 -> incrementsTTFE[5]
                in 22000..24999 -> incrementsTTFE[6]
                in 25000..27999 -> incrementsTTFE[7]
                else -> incrementsTTFE[8]
            }
        } else if (price in 30000..49999) {
            result = price + 5000
        } else if (price in 50000..99999) {
            result = price + 10000
        } else if (price in 100000..299999) {
            result = when (price) {
                //100000→120000→150000→180000→200000→220000→250000→280000→300000
                in 100000..119999 -> incrementsHTTFE[1]
                in 120000..149999 -> incrementsHTTFE[2]
                in 150000..179999 -> incrementsHTTFE[3]
                in 180000..199999 -> incrementsHTTFE[4]
                in 200000..219999 -> incrementsHTTFE[5]
                in 220000..249999 -> incrementsHTTFE[6]
                in 250000..279999 -> incrementsHTTFE[7]
                else -> incrementsHTTFE[8]
            }
        } else if (price in 300000..499999) {
            result = price + 50000
        } else {
            result = price + 100000
        }
        return result
    }

    private fun getFixedNextPrice(price: Int): Int {
        return price + fixedMarkupAmount
    }

    private fun getNextPrice(price: Int): Int {
        val result: Int
        if (price < 3000) {
            result = price + 200
        } else if (price in 3000..4999) {
            result = price + 500
        } else if (price in 5000..9999) {
            result = price + 1000
        } else if (price in 10000..29999) {
            result = price + 2000
        } else if (price in 30000..49999) {
            result = price + 5000
        } else if (price in 50000..99999) {
            result = price + 10000
        } else if (price in 100000..299999) {
            result = price + 20000
        } else if (price in 300000..499999) {
            result = price + 50000
        } else {
            result = price + 100000
        }
        return result
    }

    /** 250加价规则 */
    private fun markupRulesTHAF(type: Int) {
        var value = currentLatestPrice
        if (type == 2) {
            if (value >= 10000000) {
                RxToast.warning("价格超出上限")
                return
            }
            if (value < 3000) {
                if (value < 200) {
                    value = incrementsTHAF[0]
                } else if (value in 200..499) {
                    value = incrementsTHAF[1]
                } else if (value in 500..999) {
                    value = incrementsTHAF[2]
                } else if (value in 1000..1199) {
                    value = incrementsTHAF[3]
                } else if (value in 1200..1499) {
                    value = incrementsTHAF[4]
                } else if (value in 1500..1999) {
                    value = incrementsTHAF[5]
                } else if (value in 2000..2199) {
                    value = incrementsTHAF[6]
                } else if (value in 2200..2499) {
                    value = incrementsTHAF[7]
                } else {
                    value = incrementsTHAF[8]
                }
            } else if (value in 3000..4999) {
                value += 500
            } else if (value in 5000..9999) {
                value += 1000
            } else if (value in 10000..29999) {
                value = when (value) {
                    in 10000..11999 -> incrementsTHAFT[1]
                    in 12000..14999 -> incrementsTHAFT[2]
                    in 15000..19999 -> incrementsTHAFT[3]
                    in 20000..21999 -> incrementsTHAFT[4]
                    in 22000..24999 -> incrementsTHAFT[5]
                    in 25000..29999 -> incrementsTHAFT[6]
                    else -> incrementsTHAFT[7]
                }
            } else if (value in 30000..49999) {
                value += 5000
            } else if (value in 50000..99999) {
                value += 10000
            } else if (value in 100000..299999) {
                value = when (value) {
                    in 100000..119999 -> incrementsTHAFHT[1]
                    in 120000..149999 -> incrementsTHAFHT[2]
                    in 150000..199999 -> incrementsTHAFHT[3]
                    in 200000..219999 -> incrementsTHAFHT[4]
                    in 220000..249999 -> incrementsTHAFHT[5]
                    in 250000..299999 -> incrementsTHAFHT[6]
                    else -> incrementsTHAFHT[7]
                }
            } else if (value in 300000..499999) {
                value += 50000
            } else {
                value += 100000
            }
        } else {//减法
            if (value <= mPrice) {
                RxToast.warning("已是当前竞拍价")
                return
            }
            if (value > 500000) {
                value -= 100000
            } else if (value in 300001..500000) {
                value -= 50000
            } else if (value in 100001..300000) {
                value = when (value) {
                    in 250001..300000 -> {
                        if (mPrice in 250001..300000) {
                            mPrice
                        } else {
                            incrementsTHAFHT[5]
                        }
                    }

                    in 220001..250000 -> {
                        if (mPrice in 220001..250000) {
                            mPrice
                        } else {
                            incrementsTHAFHT[4]
                        }
                    }

                    in 200001..220000 -> {
                        if (mPrice in 200001..220000) {
                            mPrice
                        } else {
                            incrementsTHAFHT[3]
                        }
                    }

                    in 150001..200000 -> {
                        if (mPrice in 150001..200000) {
                            mPrice
                        } else {
                            incrementsTHAFHT[2]
                        }
                    }

                    in 120001..150000 -> {
                        if (mPrice in 120001..150000) {
                            mPrice
                        } else {
                            incrementsTHAFHT[1]
                        }
                    }

                    else -> {
                        if (mPrice in 100001..120000) {
                            mPrice
                        } else {
                            incrementsTHAFHT[0]
                        }
                    }
                }
            } else if (value in 50001..100000) {
                value -= 10000
            } else if (value in 30001..50000) {
                value -= 5000
            } else if (value in 10001..30000) {
                value = when (value) {
                    in 25001..30000 -> {
                        if (mPrice in 25001..30000) {
                            mPrice
                        } else {
                            incrementsTHAFT[5]
                        }
                    }

                    in 22001..25000 -> {
                        if (mPrice in 22001..25000) {
                            mPrice
                        } else {
                            incrementsTHAFT[4]
                        }
                    }

                    in 20001..22000 -> {
                        if (mPrice in 20001..22000) {
                            mPrice
                        } else {
                            incrementsTHAFT[3]
                        }
                    }

                    in 15001..20000 -> {
                        if (mPrice in 15001..20000) {
                            mPrice
                        } else {
                            incrementsTHAFT[2]
                        }
                    }

                    in 12001..15000 -> {
                        if (mPrice in 12001..15000) {
                            mPrice
                        } else {
                            incrementsTHAFT[1]
                        }
                    }

                    else -> {
                        if (mPrice in 10001..12000) {
                            mPrice
                        } else {
                            incrementsTHAFT[0]
                        }
                    }
                }
            } else if (value in 5001..10000) {
                value -= 1000
            } else if (value in 3001..5000) {
                value -= 500
            } else {
                value = when (value) {
                    in 2501..3000 -> {
                        if (mPrice in 2501..3000) {
                            mPrice
                        } else {
                            incrementsTHAF[7]
                        }
                    }

                    in 2201..2500 -> {
                        if (mPrice in 2201..2500) {
                            mPrice
                        } else {
                            incrementsTHAF[6]
                        }
                    }

                    in 2001..2200 -> {
                        if (mPrice in 2001..2200) {
                            mPrice
                        } else {
                            incrementsTHAF[5]
                        }
                    }

                    in 1501..2000 -> {
                        if (mPrice in 1501..2000) {
                            mPrice
                        } else {
                            incrementsTHAF[4]
                        }
                    }

                    in 1201..1500 -> {
                        if (mPrice in 1201..1500) {
                            mPrice
                        } else {
                            incrementsTHAF[3]
                        }
                    }

                    in 1001..1200 -> {
                        if (mPrice in 1001..1200) {
                            mPrice
                        } else {
                            incrementsTHAF[2]
                        }
                    }

                    in 501..1000 -> {
                        if (mPrice in 501..1000) {
                            mPrice
                        } else {
                            incrementsTHAF[1]
                        }
                    }

                    in 201..500 -> {
                        if (mPrice in 201..500) {
                            mPrice
                        } else {
                            incrementsTHAF[0]
                        }
                    }

                    else -> mPrice
                }
            }
            if (value <= mPrice) {
                value = mPrice
            }
        }
        currentLatestPrice = value
        mDatabind.includeView.etBiddingPrice.text = formatAmount(value.toDouble())
        setNextPrice(value)
    }

    /** 2580加价规则 */
    private fun markupRulesTFZ(type: Int) {
        var value = currentLatestPrice
        if (type == 2) {
            if (value >= 10000000) {
                RxToast.warning("价格超出上限")
                return
            }
            if (value < 3000) {
                if (value < 200) {
                    value = incrementsTFE[0]
                } else if (value in 200..499) {
                    value = incrementsTFE[1]
                } else if (value in 500..799) {
                    value = incrementsTFE[2]
                } else if (value in 800..999) {
                    value = incrementsTFE[3]
                } else if (value in 1000..1199) {
                    value = incrementsTFE[4]
                } else if (value in 1200..1499) {
                    value = incrementsTFE[5]
                } else if (value in 1500..1799) {
                    value = incrementsTFE[6]
                } else if (value in 1800..1999) {
                    value = incrementsTFE[7]
                } else if (value in 2000..2199) {
                    value = incrementsTFE[8]
                } else if (value in 2200..2499) {
                    value = incrementsTFE[9]
                } else if (value in 2500..2799) {
                    value = incrementsTFE[10]
                } else {
                    value = incrementsTFE[11]
                }
            } else if (value in 3000..4999) {
                value += 500
            } else if (value in 5000..9999) {
                value += 1000
            } else if (value in 10000..29999) {
                value = when (value) {
                    in 10000..11999 -> incrementsTTFE[1]
                    in 12000..14999 -> incrementsTTFE[2]
                    in 15000..17999 -> incrementsTTFE[3]
                    in 18000..19999 -> incrementsTTFE[4]
                    in 20000..21999 -> incrementsTTFE[5]
                    in 22000..24999 -> incrementsTTFE[6]
                    in 25000..27999 -> incrementsTTFE[7]
                    else -> incrementsTTFE[8]
                }
            } else if (value in 30000..49999) {
                value += 5000
            } else if (value in 50000..99999) {
                value += 10000
            } else if (value in 100000..299999) {
                value = when (value) {
                    in 100000..119999 -> incrementsHTTFE[1]
                    in 120000..149999 -> incrementsHTTFE[2]
                    in 150000..179999 -> incrementsHTTFE[3]
                    in 180000..199999 -> incrementsHTTFE[4]
                    in 200000..219999 -> incrementsHTTFE[5]
                    in 220000..249999 -> incrementsHTTFE[6]
                    in 250000..279999 -> incrementsHTTFE[7]
                    else -> incrementsHTTFE[8]
                }
            } else if (value in 300000..499999) {
                value += 50000
            } else {
                value += 100000
            }
        } else {//减法
            if (value <= mPrice) {
                RxToast.warning("已是当前竞拍价")
                return
            }
            if (value > 500000) {
                value -= 100000
            } else if (value in 300001..500000) {
                value -= 50000
            } else if (value in 100001..300000) {
                value = when (value) {
                    in 280001..300000 -> {
                        if (mPrice in 280001..300000) {
                            mPrice
                        } else {
                            incrementsHTTFE[7]
                        }
                    }

                    in 250001..280000 -> {
                        if (mPrice in 250001..280000) {
                            mPrice
                        } else {
                            incrementsHTTFE[6]
                        }
                    }

                    in 220001..250000 -> {
                        if (mPrice in 220001..250000) {
                            mPrice
                        } else {
                            incrementsHTTFE[5]
                        }
                    }

                    in 200001..220000 -> {
                        if (mPrice in 200001..220000) {
                            mPrice
                        } else {
                            incrementsHTTFE[4]
                        }
                    }

                    in 180001..200000 -> {
                        if (mPrice in 180001..200000) {
                            mPrice
                        } else {
                            incrementsHTTFE[3]
                        }
                    }

                    in 150001..180000 -> {
                        if (mPrice in 150001..180000) {
                            mPrice
                        } else {
                            incrementsHTTFE[2]
                        }
                    }

                    in 120001..150000 -> {
                        if (mPrice in 120001..150000) {
                            mPrice
                        } else {
                            incrementsHTTFE[1]
                        }
                    }

                    else -> {
                        if (mPrice in 100001..120000) {
                            mPrice
                        } else {
                            incrementsHTTFE[0]
                        }
                    }
                }
            } else if (value in 50001..100000) {
                value -= 10000
            } else if (value in 30001..50000) {
                value -= 5000
            } else if (value in 10001..30000) {
                value = when (value) {
                    in 28001..30000 -> {
                        if (mPrice in 28001..30000) {
                            mPrice
                        } else {
                            incrementsTTFE[7]
                        }
                    }

                    in 25001..28000 -> {
                        if (mPrice in 25001..28000) {
                            mPrice
                        } else {
                            incrementsTTFE[6]
                        }
                    }

                    in 22001..25000 -> {
                        if (mPrice in 22001..25000) {
                            mPrice
                        } else {
                            incrementsTTFE[5]
                        }
                    }

                    in 20001..22000 -> {
                        if (mPrice in 20001..22000) {
                            mPrice
                        } else {
                            incrementsTTFE[4]
                        }
                    }

                    in 18001..20000 -> {
                        if (mPrice in 18001..20000) {
                            mPrice
                        } else {
                            incrementsTTFE[3]
                        }
                    }

                    in 15001..18000 -> {
                        if (mPrice in 15001..18000) {
                            mPrice
                        } else {
                            incrementsTTFE[2]
                        }
                    }

                    in 12001..15000 -> {
                        if (mPrice in 12001..15000) {
                            mPrice
                        } else {
                            incrementsTTFE[1]
                        }
                    }

                    else -> {
                        if (mPrice in 10001..12000) {
                            mPrice
                        } else {
                            incrementsTTFE[0]
                        }
                    }
                }
            } else if (value in 5001..10000) {
                value -= 1000
            } else if (value in 3001..5000) {
                value -= 500
            } else {
                value = when (value) {
                    in 2801..3000 -> {
                        if (mPrice in 2801..3000) {
                            mPrice
                        } else {
                            incrementsTFE[10]
                        }
                    }

                    in 2501..2800 -> {
                        if (mPrice in 2501..2800) {
                            mPrice
                        } else {
                            incrementsTFE[9]
                        }
                    }

                    in 2201..2500 -> {
                        if (mPrice in 2201..2500) {
                            mPrice
                        } else {
                            incrementsTFE[8]
                        }
                    }

                    in 2001..2200 -> {
                        if (mPrice in 2001..2200) {
                            mPrice
                        } else {
                            incrementsTFE[7]
                        }
                    }

                    in 1801..2000 -> {
                        if (mPrice in 1801..2000) {
                            mPrice
                        } else {
                            incrementsTFE[6]
                        }
                    }

                    in 1501..1800 -> {
                        if (mPrice in 1501..1800) {
                            mPrice
                        } else {
                            incrementsTFE[5]
                        }
                    }

                    in 1201..1500 -> {
                        if (mPrice in 1201..1500) {
                            mPrice
                        } else {
                            incrementsTFE[4]
                        }
                    }

                    in 1001..1200 -> {
                        if (mPrice in 1001..1200) {
                            mPrice
                        } else {
                            incrementsTFE[3]
                        }
                    }

                    in 801..1000 -> {
                        if (mPrice in 801..1000) {
                            mPrice
                        } else {
                            incrementsTFE[2]
                        }
                    }

                    in 501..800 -> {
                        if (mPrice in 501..800) {
                            mPrice
                        } else {
                            incrementsTFE[1]
                        }
                    }

                    in 201..500 -> {
                        if (mPrice in 201..500) {
                            mPrice
                        } else {
                            incrementsTFE[0]
                        }
                    }

                    else -> mPrice
                }
            }
            if (value <= mPrice) {
                value = mPrice
            }
        }
        currentLatestPrice = value
        mDatabind.includeView.etBiddingPrice.text = formatAmount(value.toDouble())
        setNextPrice(value)
    }

    /** 固定加价规则 */
    private fun fixedMarkupRules(type: Int) {
        var value = currentLatestPrice
        if (type == 2) {
            if (value >= 10000000) {
                RxToast.warning("价格超出上限")
                return
            }
            value += fixedMarkupAmount
        } else {
            if (value <= mPrice) {
                RxToast.warning("已是当前竞拍价")
                return
            }
            value -= fixedMarkupAmount
        }
        currentLatestPrice = value
        mDatabind.includeView.etBiddingPrice.text = formatAmount(value.toDouble())
        setNextPrice(value)
    }

    /** 默认阶梯加价规则 */
    private fun defaultLadderMarkupRule(type: Int) {
        var value = currentLatestPrice
        if (type == 2) {
            if (value >= 10000000) {
                RxToast.warning("价格超出上限")
                return
            }
            if (value < 3000) {
                value += 200
            } else if (value in 3000..4999) {
                value += 500
            } else if (value in 5000..9999) {
                value += 1000
            } else if (value in 10000..29999) {
                value += 2000
            } else if (value in 30000..49999) {
                value += 5000
            } else if (value in 50000..99999) {
                value += 10000
            } else if (value in 100000..299999) {
                value += 20000
            } else if (value in 300000..499999) {
                value += 50000
            } else {
                value += 100000
            }
        } else {//减法
            if (value <= mPrice) {
                RxToast.warning("已是当前竞拍价")
                return
            }
            if (value > 500000) {
                value -= 100000
            } else if (value in 300001..500000) {
                value -= 50000
            } else if (value in 100001..300000) {
                value -= 20000
            } else if (value in 50001..100000) {
                value -= 10000
            } else if (value in 30001..50000) {
                value -= 5000
            } else if (value in 10001..30000) {
                value -= 2000
            } else if (value in 5001..10000) {
                value -= 1000
            } else if (value in 3001..5000) {
                value -= 500
            } else {
                value -= 200
            }
        }
        currentLatestPrice = value
        mDatabind.includeView.etBiddingPrice.text = formatAmount(value.toDouble())
        setNextPrice(value)
    }
}